# -*- coding: utf-8 -*-
# @Time : 2020/10/31 11:15
# @Author : Houor
# @Site : https://my.oschina.net/houor
# @File : success_failure_search.py
# @Description: 成功失败搜索区间

from ods.functions import tri_equation

h = 0.5
x = -0.5
idx = 1
flag = False

func = tri_equation

# the interval may not be found

c = 0
d = 0

a1 = x
a2 = a1 + h

f1 = func(a1)
f2 = func(a2)

print('idx = ', idx, ':')
print('\t\ta1 = ', a1, ', a2 = ', a2, ', h = ', h)
print('\t\tf1 = ', f1, ', f2 = ', f2)
idx = idx + 1

if f1 > f2:
    while not flag:

        h = 2 * h
        a2 = a2 + h
        f1 = f2
        f2 = func(a2)

        if f1 > f2:
            a1 = a2 - h
        else:
            c = a1
            d = a2
            flag = True

        print('idx = ', idx, ':')
        print('\t\ta1 = ', a1, ', a2 = ', a2, ', h = ', h)
        print('\t\tf1 = ', f1, ', f2 = ', f2)
        idx = idx + 1
else:
    h = -0.25 * h
    while not flag:
        a1 = a1 + h
        f2 = f1
        f1 = func(a1)

        if f1 < f2:
            a2 = a1 - h
            h = h + h
        else:
            c = a1
            d = a2
            flag = True

        print('idx = ', idx, ':')
        print('\t\ta1 = ', a1, ', a2 = ', a2, ', h = ', h)
        print('\t\tf1 = ', f1, ', f2 = ', f2)
        idx = idx + 1

print("c = ", c, ", d = ", d)
